package servlet.log;

import bean.dto.LogQueryPage;
import bean.dto.PermissionQueryPage;
import bean.pojo.Permissions;
import bean.vo.LogsVO;
import bean.vo.PermissionsOption;
import bean.vo.RouterVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.LogService;
import service.PermissionService;
import service.impl.LogServiceImpl;
import service.impl.PermissionServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;

/**
 * 日志管理
 */
@WebServlet(name = "LogManagement", value = "/log/*")
public class LogManagement extends BaseServlet {
    /**
     * 权限分页查询
     *
     * @param request
     * @return
     */
    public R findByPage(HttpServletRequest request) {
        String str_page = request.getParameter("page");
        System.out.println("PermissionManagement:page" + str_page);
        String str_limit = request.getParameter("limit");
        System.out.println("PermissionManagement:limit" + str_limit);
        LogQueryPage queryPage = new LogQueryPage();
        if (str_page.trim().length() > 0) {
            try {
                queryPage.setPage(Integer.parseInt(str_page));
            } catch (Exception e) {
                queryPage.setPage(1);
            }
        }
        //设置每页行数
        if (str_limit.trim().length() > 0) {
            try {
                queryPage.setLimit(Integer.parseInt(str_limit));
            } catch (Exception e) {
                queryPage.setLimit(10);
            }
        }
        //设置查询条件
        queryPage.setOperationType(request.getParameter("operationType"));
        queryPage.setUserName(request.getParameter("userName"));

        System.out.println("LogManagement:findByPage:queryPage:" + queryPage);
        LogService logService = new LogServiceImpl();
        //满足条件的总条数
        Long total = logService.findByTotal(queryPage);
        System.out.println("LogManagement:findByPage:total:" + total);
        //当前页码的数据
        List<LogsVO> list = null;
        if (total > 0) {
            //当前页码的数据
            list = logService.findByPage(queryPage);
            System.out.println("LogManagement:findByPage:list:" + list.toString());
        }
        //Data?     Long       addData把data HashMap
        R r = R.ok().addData("total", total).addData("list", list);
        return r;
    }


    /**
     * 根据id删除日志
     *
     * @param request
     * @return
     */
    public R deleteLogById(HttpServletRequest request) {
        String id = request.getParameter("logId");
        System.out.println(id);
        R r;
        Integer logId = null;
        try {
            logId = Integer.parseInt(id);
            LogService logService = new LogServiceImpl();
            boolean update = logService.deleteById(logId);
            r = !update ? R.error("删除失败") : R.ok();
        } catch (Exception e) {
            r = R.error("ID编号不正确");
        }
        return r;
    }

    /**
     * 删除所有日志
     *
     * @param request
     * @return
     */
    public R deleteAllLogs(HttpServletRequest request) {
        R r;
        LogService logService = new LogServiceImpl();
        boolean update = logService.deleteAllLogs();
        r = !update ? R.error("删除失败") : R.ok();
        return r;
    }

}
